/**
 * 大客户项目维护
 * @author liuwei
 * @date 2019/11/22
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            isDetail: false,
            form: {
                GWGA002: null,
                GWGF002_NAME: null,
                GWGF007_NAME: null,
                GWGF008_NAME: null,
                GWGFORIG_NAME: null,
                GWGFORIU_NAME: null
            },
            rules: {
                GWGF001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGF002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGF003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGF004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGF005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGF007: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGF008: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGF009: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGF010: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGF011: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGA002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGFORIU: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGFORIG: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
            },
            selectOptions: {
                sys_n_y: app.datas_('sys_n_y'),
                gwgf004Options: app.datas_('gwgf004')

            },
            detailReadOnly: false,
            secPartReadOnly: true,
            thirdPartReadOnly: true,
            fourthPartReadOnly: true
        },
        callObj,
        pageVue,
        layOpenIndex,
        moduleName = "大客户项目"
    ;

    var gwgi003Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwgi003_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        editData: function () {
            app.ajax({
                xurl: 'crm/gwgi003/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        // callObj.update(result.xdata);
                        app.pageModule('gwgi003').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },
        addData: function () {
            app.ajax({
                xurl: 'crm/gwgi003/add',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwgi003').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },
        _call: function (tagData) {
            pageVue.$refs['form'].resetFields();
            layOpenIndex = tagData.layOpenIndex;

            if (tagData.is_add) {
                vueData.isAdd = true;
                vueData.detailReadOnly = false;
                app.ajax({
                    xurl: 'crm/gwgi003/getAddData',
                    cb: function (result) {
                        if (result.ok) {
                            var resData = result.data.data;
                            app.extendUpLow({
                                target: vueData.form,
                                res: {
                                    GWGFORIU: resData.CURRENT_USER_ID,
                                    GWGFORIU_NAME: resData.CURRENT_USER_NAME,
                                    GWGFORIG: resData.CURRENT_ORG,
                                    GWGFORIG_NAME: resData.CURRENT_ORG_NAME,
                                    GWGFACTI: "Y"
                                },
                                reset: true
                            });
                        }
                    }
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                vueData.detailReadOnly = tagData.detailReadOnly;

                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                self.changeReadOnlyByGwgf004(callObj.data.GWGF004);
            }

        },

        changeReadOnlyByGwgf004(gwgf004) {
            vueData.secPartReadOnly = true;
            vueData.thirdPartReadOnly = true;
            vueData.fourthPartReadOnly = true;
            switch (gwgf004) {
                case "2":
                    vueData.secPartReadOnly = false;
                    break;
                case "3":
                    vueData.secPartReadOnly = false;
                    vueData.thirdPartReadOnly = false;
                    break;
                case "4":
                    vueData.secPartReadOnly = false;
                    vueData.thirdPartReadOnly = false;
                    vueData.fourthPartReadOnly = false;
            }
        },

        getRules : function(status) {
            if(status) {
                return new Array( {required: false, message: '不可为空', trigger: 'change'});;
            }else {
                return new Array( {required: true, message: '不可为空', trigger: 'change'});
            }
        },
        _ready: function () {
            vueData.form = app.fields_(['GWGF_T'], vueData.form);
            pageVue = new Vue({
                el: '#gwgi003_edit',
                data: vueData,
                methods: {
                    openGwgf001: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwga001',
                                    searchParam: {
                                        GWGAACTI: 'Y'
                                    },
                                    columnList: [
                                        {label: '招标编号', prop: 'GWGA001', width: '150'},
                                        {label: '项目名称', prop: 'GWGA002', width: '150'},
                                        {label: '所属区域', prop: 'GWGA003_NAME', width: '150'},
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGF001 = data.GWGA001;
                                    vueData.form.GWGA002 = data.GWGA002;
                                    vueData.form.GWGF002 = data.GWGA003;
                                    vueData.form.GWGF002_NAME = data.GWGA003_NAME;
                                    vueData.form.GWGF003 = data.GWGA006;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择招标编号',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    openGwgf007: function () {
                        var keyArrayStr = vueData.form.GWGF007;
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                multiple: true,
                                config: {
                                    method: 'q_gwak001',
                                    columnList: [
                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'}
                                    ],
                                    dataList: [],
                                    key: 'GWAK001'
                                },
                                cb: function (data) {
                                    console.log(data);
                                    let GWGF007_ARR = [];
                                    let GWGF007_NAME_ARR = [];
                                    for (var i = 0; i < data.length; i++) {
                                        GWGF007_ARR.push(data[i].GWAK001);
                                        GWGF007_NAME_ARR.push(data[i].GWAKL003);
                                    }
                                    vueData.form.GWGF007 = GWGF007_ARR.join(";");
                                    vueData.form.GWGF007_NAME = GWGF007_NAME_ARR.join(";");
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择入围单位',
                                area: ['600px', '550px']
                            }
                        });
                    },
                    clearGwgf007: function () {
                        vueData.form.GWGF007 = null;
                        vueData.form.GWGF007_NAME = null;
                        vueData.form.GWGF008 = null;
                        vueData.form.GWGF008_NAME = null;
                    },
                    openGwgf008: function () {
                        if (vueData.form.GWGF007 == null || vueData.form.GWGF007 === '') {
                            app.showWarning("请先选择入围单位");
                            return;
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'cq_gwak001_5',
                                    searchParam: {
                                        keyArrayStr: vueData.form.GWGF007
                                    },
                                    columnList: [
                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150', sortable: false, unshow: true}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGF008 = data.GWAK001;
                                    vueData.form.GWGF008_NAME = data.GWAKL004;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择中标单位',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    clearGwgf008: function () {
                        vueData.form.GWGF008 = '';
                        vueData.form.GWGF008_NAME = '';
                    },
                    cancel: function () {
                        layer.close(layOpenIndex);
                    },
                    saveData: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    }
                },
                watch: {
                    'form.GWGF004': function (newValue) {  // 项目阶段
                        self.changeReadOnlyByGwgf004(newValue)
                    },
                    'form.GWGF005': function (newValue, oldValue) {  // 金额
                        if (newValue == null) return;
                        var re = /^-?\d+\.?\d{0,2}$/; //判断是否最多2位小数的数字类型
                        if ((!re.test(newValue)) || parseFloat(newValue) <= 0) {
                            app.showWarning("预算为数字类型且小数位数最多为2位！");
                            vueData.form.GWGF005 = '';
                        }
                    },
                    'form.GWGF009': function (newValue, oldValue) {  // 金额
                        if (newValue == null) return;
                        var re = /^-?\d+\.?\d{0,2}$/; //判断是否最多2位小数的数字类型
                        if ((!re.test(newValue)) || parseFloat(newValue) <= 0) {
                            app.showWarning("中标金额为数字类型且小数位数最多为2位！");
                            vueData.form.GWGF009 = '';
                        }
                    }
                },
                computed: {
                    secPartRules: function() {
                       return self.getRules(vueData.secPartReadOnly);
                    },
                    thirdPartRules: function() {
                       return self.getRules(vueData.thirdPartReadOnly);
                    },
                    fourthPartRules: function() {
                       return self.getRules(vueData.fourthPartReadOnly);
                    }
                },
                created: function () {
                    self._setEleIsReady('gwgi003_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });
    return new gwgi003Edit();
});